package com.flyduck.mall.oms.manager;

import com.flyduck.mall.oms.mapper.OmsOrderMapper;

import com.flyduck.mall.common.core.utils.BeanUtils;
import com.flyduck.ss.common.core.exception.ServiceException;
import com.flyduck.ss.common.core.utils.StringUtils;
import com.flyduck.ss.common.satoken.utils.LoginHelper;
import com.flyduck.mall.oms.api.constant.OrderConstant;
import com.flyduck.mall.oms.domain.OmsOrder;
import com.flyduck.mall.oms.domain.OmsOrderItem;
import com.flyduck.mall.oms.mapper.OmsOrderItemMapper;
import com.flyduck.mall.pms.api.RemoteSkuStockService;
import com.flyduck.mall.pms.api.vo.RemoteSkuStockReleaseReqVO;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * AppOrderManager
 * </p>
 *
 * @author flyduck
 * @since 2024-05-21
 */
@Component
public class OmsOrderManager {

    @Resource
    private OmsOrderMapper omsOrderMapper;
    @DubboReference
    private RemoteSkuStockService remoteSkuStockService;
    @Resource
    private OmsOrderItemMapper omsOrderItemMapper;

    public OmsOrder verifyAppOrderBySn(String sn){
        Long userId = LoginHelper.getUserId();

        OmsOrder omsOrder = omsOrderMapper.getAppOrderBySn(sn);
        if(omsOrder == null){
            throw new ServiceException("订单不存在");
        }
        if (!userId.equals(omsOrder.getUserId())) {
            throw new ServiceException("无权限操作该订单");
        }
        return omsOrder;
    }

    public OmsOrder verifyAppOrderById(Long id){
        Long userId = LoginHelper.getUserId();

        OmsOrder omsOrder = omsOrderMapper.selectById(id);
        if(omsOrder == null){
            throw new ServiceException("订单不存在");
        }
        if (!userId.equals(omsOrder.getUserId())) {
            throw new ServiceException("无权限操作该订单");
        }
        return omsOrder;
    }

    @GlobalTransactional(rollbackFor = Exception.class)
    public void closeOrder(Long orderId, String adminNode, Long userId) {
        OmsOrder omsOrder = omsOrderMapper.selectById(orderId);
        if (omsOrder == null) {
            //订单已被手动删除。mq消息不需要做任何处理
            return;
        }
        if (userId == null) {
            throw new ServiceException("管理员不能为空");
        }
        if (StringUtils.isBlank(adminNode)) {
            throw new ServiceException("管理员备注不能为空");
        }

        if (OrderConstant.OrderStatus.PENDING_PAYMENT.getCode() != omsOrder.getOrderStatus()) {
            //订单状态不为待支付。mq消息不需要做任何处理（只有待支付的状态才可以被关闭）
            return;
        }

        omsOrder.setOrderStatus(OrderConstant.OrderStatus.CANCELLED.getCode());
        omsOrder.setUpdateTime(new Date());
        omsOrder.setAdminNote("管理员：" + userId + "关闭：" + adminNode);
        omsOrderMapper.updateById(omsOrder);

        //关闭订单后需要释放库存
        List<OmsOrderItem> omsOrderItemList = omsOrderItemMapper.getAppOrderItemListByOrderId(orderId);
        List<RemoteSkuStockReleaseReqVO> remoteSkuStockReleaseReqVOList = BeanUtils.toBeanList(omsOrderItemList, RemoteSkuStockReleaseReqVO.class);
        remoteSkuStockService.batchReleaseSkuStock(remoteSkuStockReleaseReqVOList);
    }
}
